home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewMaxwell-c / Original / movebits.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  4.4 KB  |  175 lines  |  [TEXT/KAHL]

  1. /*
  2.  * move or draw ball on screen
  3.  */
  4.  
  5. #include <qd.h>
  6. #include <qdvars.h>
  7. #include "ball.h"
  8.  
  9. /*
  10.  * There are three off-screen bitmaps.  The first two hold the images of
  11.  * a slow ball and a fast ball.  The third is used for combining the
  12.  * before and after pictures so that a screen update of a moving ball
  13.  * whose new postion overlaps its old position can be done in one
  14.  * screen operation.  This is intended to reduce flicker, by cutting
  15.  * number of copybits calls to the screen from two to one per slow
  16.  * moving ball.  However, it increases the total number of copybits
  17.  * calls, which makes things run slower.
  18.  *
  19.  * The use of this third bitmap is enabled by defining SMOOTH.  It will
  20.  * run ~25% slower with SMOOTH defined.
  21.  */
  22.  
  23. char sbits[ 2*(SDIM)*((SDIM+15)/16) ];        /* slow bits */
  24. char fbits[ 2*(SDIM)*((SDIM+15)/16) ];        /* fast bits */
  25. #ifdef SMOOTH
  26. char dbits[ 2*(2*SDIM)*((2*SDIM+15)/16) ];    /* combined bits */
  27. #endif
  28.  
  29. BitMap sm, fm                            /* bitmaps for slow and fast bits */
  30. #ifdef SMOOTH
  31.     ,dm                                    /* and combined bits */
  32. #endif
  33. ;
  34.  
  35. /*
  36.  * movebits() draws two balls.  The first is in the square with corner
  37.  * at Sx, Sy, and side SDIM.  The second is at Dx and Dy, and has the
  38.  * same size.  Which drawing to use for each is determined by op and np.
  39.  */
  40. movebits( win, Sx, Sy, Dx, Dy, op, np )
  41.     GrafPtr win;
  42.     long Sx, Sy, Dx, Dy, op, np;
  43. {
  44.     register int sx, sy, dx, dy;
  45.     int tx,ty;
  46.     int rt, rl;
  47.     Rect S,D;
  48.  
  49.     /*
  50.      * The pointers to the bits in the bitmaps may have changed if we
  51.      * have been moved on the heap since we were initialized, so we shall
  52.      * fix them.
  53.      */
  54.     sm.baseAddr = sbits;
  55.     fm.baseAddr = fbits;
  56. #ifdef SMOOTH
  57.     dm.baseAddr = dbits;
  58. #endif
  59.     
  60.     sx = Sx; sy = Sy; dx = Dx; dy = Dy; /* convert to integers */
  61.     tx = dx - sx;                        /* relative x positions */
  62.     ty = dy - sy;                        /* relative y positions */
  63.     
  64.     
  65. #define pict(v) ((v) == 0 ? &sm : &fm )
  66.  
  67.     SetPort( win );
  68.  
  69. /*
  70.  * If the balls do not overlap, or if SMOOTH is not defined, then we
  71.  * simply want to erase the first ball and draw the second.
  72.  */
  73. #ifdef SMOOTH
  74.     /*
  75.      * If the balls can't possibly overlap, then just draw them directly
  76.      * on the screen
  77.      */
  78.     if ( tx < -GRAD || tx > GRAD || ty < -GRAD || ty > GRAD ) {
  79. #endif
  80.         SetRect( &S, 0, 0, SDIM, SDIM );
  81.         SetRect( &D, sx, sy, sx + SDIM, sy + SDIM );
  82.         CopyBits( pict(op), &win->portBits, &S, &D, srcXor, 0L );
  83.         SetRect( &D, dx, dy, dx + SDIM, dy + SDIM );
  84.         CopyBits( pict(np), &win->portBits, &S, &D, srcXor, 0L );
  85.         return;
  86. #ifdef SMOOTH
  87.     }
  88.     
  89.     /*
  90.      * The balls are close enough that we may combine their updates into
  91.      * one CopyBits to the screen.
  92.      */
  93.     
  94.     /*
  95.      * The rest of this is a pain to explain without a diagram,
  96.      * so figure it out for yourself!
  97.      */
  98.     if ( ty > 0 )
  99.         if ( tx > 0 )    { rt = 0;    rl = 0; }
  100.         else            { rt = 0;    rl = SDIM; }
  101.     else
  102.         if ( tx > 0 )    { rt = SDIM; rl = 0; }
  103.         else            { rt = SDIM; rl = SDIM; }
  104.         
  105.     SetRect( &D, 0, 0, 2*SDIM, 2*SDIM );
  106.     CopyBits( &dm, &dm, &D, &D, srcXor, 0L );
  107.     SetRect( &S, 0, 0, SDIM, SDIM );
  108.     SetRect( &D, rl, rt, rl+SDIM, rt+SDIM );
  109.     CopyBits( pict(op), &dm, &S, &D, srcCopy, 0L );
  110.     SetRect( &D, rl + tx, rt + ty, rl + tx + SDIM, rt + ty + SDIM );
  111.     CopyBits( pict(np), &dm, &S, &D, srcXor, 0L );
  112.     SetRect( &S, 0, 0, SDIM*2, SDIM*2 );
  113.     SetRect( &D, sx - rl, sy - rt, sx - rl + 2*SDIM, sy - rt + 2*SDIM );
  114.     CopyBits( &dm, &win->portBits, &S, &D, srcXor, 0L );
  115. #endif
  116. }
  117.  
  118. /*
  119.  * Initialize the bitmaps
  120.  */    
  121. initmove() {        
  122.     Rect S,D;
  123.     GrafPort port;
  124.  
  125.     /*
  126.      * get a grafport to do our thing in
  127.      */
  128.     OpenPort( &port );
  129.     PenNormal();
  130.     
  131.     /*
  132.      * bitmap for slow bits...
  133.      */
  134.     sm.baseAddr = sbits;
  135.     sm.rowBytes = 2*((SDIM+15)/16);
  136.     sm.bounds.a.top = 0;
  137.     sm.bounds.a.bottom = 2*SDIM;
  138.     sm.bounds.a.left = 0;
  139.     sm.bounds.a.right = 2*SDIM;
  140.     
  141.     /*
  142.      * bitmap for fast bits...
  143.      */
  144.     fm.baseAddr = fbits;
  145.     fm.rowBytes = 2*((SDIM+15)/16);
  146.     fm.bounds.a.top = 0;
  147.     fm.bounds.a.bottom = 2*SDIM;
  148.     fm.bounds.a.left = 0;
  149.     fm.bounds.a.right = 2*SDIM;
  150.     
  151.     SetPortBits( &sm );                /* prepare to draw slow ball */
  152.     
  153.     SetRect( &S, 0, 0, SDIM, SDIM );
  154.     FrameOval( &S );
  155.     MoveTo( 3, 7 ); LineTo( 4, 8 );    /* ...risking the taste-police */
  156.     LineTo( 7, 8 ); LineTo( 8, 7 );
  157.     MoveTo( 4, 4 ); LineTo( 4, 4 );
  158.     MoveTo( 7, 4 ); LineTo( 7, 4 );
  159.  
  160.     SetPortBits( &fm );                /* prepare to draw fast ball */
  161.     InvertOval( &S );
  162.     
  163. #ifdef SMOOTH
  164.     /*
  165.      * bitmap for combined drawings...
  166.      */
  167.     dm.baseAddr = dbits;
  168.     dm.rowBytes = 2*((SDIM+SDIM+15)/16);
  169.     dm.bounds.a.top = 0;
  170.     dm.bounds.a.bottom = 2*SDIM;
  171.     dm.bounds.a.left = 0;
  172.     dm.bounds.a.right = 2*SDIM;
  173. #endif    
  174. }
  175.